import request from "@/utils/request";

const roleApi = new Map([
    ["list", "role/list"],
    ["save", "role/save"],
    ["update", "role/save"],
    ["delete", "role/delete"],
    ["users", "role/users"],
    ["permissions","role/perms"],
    ["assignPermissions", "role/assign_perms"],
    ["deletePermissions", "role/unassign_perms"]
]);

/**
 * 角色用户信息
 * 
 * @param {number} role_id 角色id
 * @returns {Promise}
 */
const roleUsers = (role_id) => {
    return request.get(roleApi.get('users'),{
        params:{
            role_id
        }
    });
}

/**
 * 角色权限信息
 * 
 * @param {number} role_id 角色id
 * @returns {Promise}
 */
const rolePermissions = (role_id) => {
    return request.get(roleApi.get('permissions'),{
        params:{
            role_id
        }
    });
}

/**
 * 所有角色信息
 * 
 * @param {number} limit 每页展示数据个数
 * @param {number} page 当前页码
 * @param {string} name 用户名
 * @returns {Promise}
 */
const roleList = (limit,page,name) => {
    return request.get(roleApi.get('list'),{
        params:{
            limit,
            page,
            name
        }
    });
}

/**
 * 添加角色
 * 
 * @param {Object} data
 * @param {string} data.name 角色名
 * @param {string} data.description 角色描述
 * @param {number} data.status 角色状态(1启用,0为禁用)
 * @returns {Promise}
 */
const roleAdd = (data) => {
    return request.post(roleApi.get('save'),data);
}

/**
 * 修改角色
 * 
 * @param {Object} data
 * @param {number} data.id 角色id
 * @param {string} data.name 角色名
 * @param {string} data.description 角色描述
 * @param {number} data.status 角色状态(1启用,0为禁用)
 * @returns {Promise}
 */
const roleUpdate = (data) => {
    return request.post(roleApi.get('update'),data);
}

/**
 * 删除角色
 * 
 * @param {number} id 角色id
 * @returns {Promise}
 */
const roleDelete = (id) => {
    return request.delete(roleApi.get('delete'),{
        params:{
            id
        }
    });
}

/**
 * 批量删除角色
 * 
 * @param {Array} idArray 角色id数组
 * @returns {Promise}
 */
const roleAllDelete = (idArray) => {
    return request.delete(roleApi.get('delete'),{
        data:{
            idArray
        }
    });
}

/**
 * 添加权限
 * 
 * @param {Object} data 
 * @param {number} data.role_id 角色id
 * @param {Array} data.perm_id 权限id 
 * @returns {Promise}
 */
const roleAddPermissions = (data) => {
    return request.post(roleApi.get('assignPermissions'),data);
}

/**
 * 删除权限
 * 
 * @param {Object} data 
 * @param {number} data.role_id 角色id
 * @param {Array} data.perm_id 权限id 
 * @returns {Promise}
 */
const roleRemovePermissions = (data) => {
    return request.delete(roleApi.get('deletePermissions'),{
        params:{
            role_id:data.role_id,
            perm_id:data.perm_id
        }
    });
}

export {
    roleAdd,
    roleDelete,
    roleUpdate,
    roleList,
    roleUsers,
    rolePermissions,
    roleAddPermissions,
    roleRemovePermissions,
    roleAllDelete
}
